﻿using System;
using System . Collections . Generic;
using System . Collections . ObjectModel;
using System . Reflection;
using System . IO;
using System . Threading . Tasks;
using System . Linq;
using System . Xml . Linq;
using System . Xml;
using System . Text;


namespace WenceyWang . RichMan4L . Maps
{
	/// <summary>
	/// 指示地图的访问代理
	/// </summary>
	public class MapProxy
	{
		public string Name { get; set; }

		public string FileName { get; set; }

		public Uri Image { get; set; }

		Map map = null;

		public Map Map
		{
			get
			{
				return map ?? ( map = new Map ( FileName ) );
			}
		}

		public MapProxy ( XElement element )
		{
			Name = element . Attribute ( nameof ( Name ) ) . Value;
			FileName = element . Attribute ( nameof ( FileName ) ) . Value;
			Image = new Uri ( element . Attribute ( nameof ( Image ) ) . Value );

		}

		static List<MapProxy> maps = null;

		public static List<MapProxy> GetMaps ( )
		{

			if ( maps == null )
			{
				LoadMaps ( ) . Wait ( );
			}
			return maps;

		}

		public static Task LoadMaps ( )
		{
			Task loadTask = new Task ( ( ) =>
			{
				maps = new List<MapProxy> ( );

				XDocument doc = ResourceHelper . LoadXmlDocument ( @"MapsResource.Index.xml" );

				foreach ( var item in doc . Root . Elements ( ) )
				{
					maps . Add ( new MapProxy ( item ) );
				}

			} );
			loadTask . Start ( );
			return loadTask;
		}

	}
}
